package com.woniu.sharepark.util.Calculate;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * double类型计算util
 * @author DELL
 * @date 2019/6/14 9:55
 * version v1.0
 */
public class CalculateUtil {

    /**
    * 加
    * @title add
    * @param a1, a2
    * @return double
    * @author DELL
    * @date 2019/6/14 9:57
    **/

    public static double add(double a1,double b1){
        BigDecimal a2 = new BigDecimal(Double.toString(a1));
        BigDecimal b2 = new BigDecimal(Double.toString(b1));

        return a2.add(b2).doubleValue();
    }


    /**
    * 减
    * @title sub
    * @param a1, a2
    * @return double
    * @author DELL
    * @date 2019/6/14 10:01
    **/

    public static double sub(Double a1,Double b1){
      BigDecimal a2 = new BigDecimal(a1);
      BigDecimal b2 = new BigDecimal(b1);

      return a2.subtract(b2).doubleValue();
    }

    /**
    * 乘
    * @title mul
    * @param a1, b1
    * @return double
    * @author DELL
    * @date 2019/6/14 10:03
    **/

    public static double mul(double a1, double b1) {
        BigDecimal a2 = new BigDecimal(Double.toString(a1));
        BigDecimal b2 = new BigDecimal(Double.toString(b1));

        return a2.multiply(b2).doubleValue();
    }

    /**
    * 保留两位小数
    * @title kepDecimal
    * @param a1
    * @return double
    * @author DELL
    * @date 2019/6/14 10:06
    **/

    public static double kepDecimal(Double a1){
        DecimalFormat df   = new DecimalFormat("######0.00");

        return Double.parseDouble(df.format(a1));
    }
    
}
